package com.cc.study.di;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import java.util.Arrays;

public class TestDI {
    //被实例化了
    //配置文件加载
    //di add book
    @Test
    public void demo01(){
        //从spring容器获得
        String xmlPath = "di.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        //BookService bookService = (BookService) applicationContext.getBean("bookServiceId");
        //通过这种方式不需要进行强制转换
        System.out.println("配置文件加载");
        BookService bookService = applicationContext.getBean("bookServiceId",BookService.class);
        bookService.addBook();
    }

    //配置文件被加载来
    //被实例化了
    //di add book
    @Test
    public void demo02(){
        //延迟加载
        //使用BeanFactory  --第一次调用getBean实例化
        String xmlPath = "di.xml";

        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(xmlPath));

        System.out.println("配置文件被加载来");
        BookService bookService = (BookService) beanFactory.getBean("bookServiceId");
        bookService.addBook();

    }

    //Spring 基于构造函数的依赖注入
    @Test
    public void demo03(){
        String xmlPath = "di2.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        TextEditor textEditor = applicationContext.getBean("textEditor",TextEditor.class);
        textEditor.spellCheck();
    }

    @Test
    public void demo04(){
        String xmlPath = "di3.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        Foo foo = applicationContext.getBean("foo",Foo.class);
    }

    @Test
    public void demo05(){
        String xmlPath = "di3.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        Foo foo = applicationContext.getBean("foo2",Foo.class);
    }

    @Test
    public void demo06(){
        String xmlPath = "di3.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        Foo foo = applicationContext.getBean("foo3",Foo.class);
    }

    @Test
    public void demo07(){
        String xmlPath = "di4.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        User user = applicationContext.getBean("userId1",User.class);
    }


    @Test
    public void demo08(){
        String xmlPath = "di4.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        User user = applicationContext.getBean("userId2",User.class);
    }

    @Test
    public void demo09(){
        String xmlPath = "di4.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        User user = applicationContext.getBean("userId3",User.class);
    }

    @Test
    public void demo10(){
        String xmlPath = "di5.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        Person p = applicationContext.getBean("personId",Person.class);
        System.out.println(p.getPname());
        System.out.println(p.getAge());
        System.out.println(p.getCompanyAddr());
        System.out.println(p.getHomeAddr());
    }

    @Test
    public void demo11(){
        String xmlPath = "di6.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        Person p = applicationContext.getBean("personId",Person.class);
        System.out.println(p.getPname());
        System.out.println(p.getAge());
        System.out.println(p.getCompanyAddr());
        System.out.println(p.getHomeAddr());
    }

    @Test
    public void demo12(){
        String xmlPath = "di7.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        CollData c = applicationContext.getBean("collDataId",CollData.class);
        System.out.println(Arrays.asList(c.getArrayData()));
        System.out.println(c.getListData());
        System.out.println(c.getSetData());
        System.out.println(c.getMapData());
        System.out.println(c.getPropsData());
        System.out.println(c.getStr());
    }

}
